package com.hung.trade.coupon.api;

import com.hung.trade.common.api.ICouponApi;
import com.hung.trade.common.constants.TradeEnums;
import com.hung.trade.common.protocol.coupon.ChangeCouponReq;
import com.hung.trade.common.protocol.coupon.ChangeCouponResp;
import com.hung.trade.common.protocol.coupon.QueryCouponReq;
import com.hung.trade.common.protocol.coupon.QueryCouponResp;
import com.hung.trade.coupon.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author hungki
 * @createTime 2018-10-09 下午 2:55
 */
@Controller
public class CouponApiImpl implements ICouponApi {
    @Autowired
    private ICouponService iCouponService;
    @Override
    @ResponseBody
    @RequestMapping(value = "/queryCoupon",method = RequestMethod.POST)
    public QueryCouponResp queryCoupon(@RequestBody QueryCouponReq queryCouponReq) {
        QueryCouponResp resp =new QueryCouponResp();
        try{
            resp = iCouponService.queryCoupon(queryCouponReq);
        }catch (Exception e){
            e.printStackTrace();
            resp .setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            resp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc()+":"+e.getMessage());
        }

        return resp;
    }

    @Override
    @ResponseBody
    @RequestMapping(value = "/changeCoupon",method = RequestMethod.POST)
    public ChangeCouponResp changeCoupon(@RequestBody ChangeCouponReq changeCouponReq) {
        System.out.println("changeCouponReq = " + changeCouponReq);
        ChangeCouponResp resp = new ChangeCouponResp();
        try {
            resp = iCouponService.changeCoupon(changeCouponReq);
        }catch (Exception e){
            e.printStackTrace();
            resp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            resp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc()+":"+e.getMessage());
        }
        return  resp;
    }
}
